/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ws.converter;

import entidade.persistencia.Empresa;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import entidade.persistencia.AreaAtuacao;
import entidade.persistencia.Usuario;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author victorinox
 */

@XmlRootElement(name = "empresa")
public class EmpresaConverter {
    private Empresa entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of EmpresaConverter */
    public EmpresaConverter() {
        entity = new Empresa();
    }

    /**
     * Creates a new instance of EmpresaConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public EmpresaConverter(Empresa entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getEmpresaId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getUsuarioCollection();
        getAreaAtuacaoId();
        getEmpresaCollection();
        getMatrizId();
    }

    /**
     * Creates a new instance of EmpresaConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public EmpresaConverter(Empresa entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for empresaId.
     *
     * @return value for empresaId
     */
    @XmlElement
    public Integer getEmpresaId() {
        return (expandLevel > 0) ? entity.getEmpresaId() : null;
    }

    /**
     * Setter for empresaId.
     *
     * @param value the value to set
     */
    public void setEmpresaId(Integer value) {
        entity.setEmpresaId(value);
    }

    /**
     * Getter for nome.
     *
     * @return value for nome
     */
    @XmlElement
    public String getNome() {
        return (expandLevel > 0) ? entity.getNome() : null;
    }

    /**
     * Setter for nome.
     *
     * @param value the value to set
     */
    public void setNome(String value) {
        entity.setNome(value);
    }

    /**
     * Getter for cnpj.
     *
     * @return value for cnpj
     */
    @XmlElement
    public String getCnpj() {
        return (expandLevel > 0) ? entity.getCnpj() : null;
    }

    /**
     * Setter for cnpj.
     *
     * @param value the value to set
     */
    public void setCnpj(String value) {
        entity.setCnpj(value);
    }

    /**
     * Getter for inscricaoEstadual.
     *
     * @return value for inscricaoEstadual
     */
    @XmlElement
    public String getInscricaoEstadual() {
        return (expandLevel > 0) ? entity.getInscricaoEstadual() : null;
    }

    /**
     * Setter for inscricaoEstadual.
     *
     * @param value the value to set
     */
    public void setInscricaoEstadual(String value) {
        entity.setInscricaoEstadual(value);
    }

    /**
     * Getter for inscricaoMunicipal.
     *
     * @return value for inscricaoMunicipal
     */
    @XmlElement
    public String getInscricaoMunicipal() {
        return (expandLevel > 0) ? entity.getInscricaoMunicipal() : null;
    }

    /**
     * Setter for inscricaoMunicipal.
     *
     * @param value the value to set
     */
    public void setInscricaoMunicipal(String value) {
        entity.setInscricaoMunicipal(value);
    }

    /**
     * Getter for dataUltimaAlteracao.
     *
     * @return value for dataUltimaAlteracao
     */
    @XmlElement
    public Date getDataUltimaAlteracao() {
        return (expandLevel > 0) ? entity.getDataUltimaAlteracao() : null;
    }

    /**
     * Setter for dataUltimaAlteracao.
     *
     * @param value the value to set
     */
    public void setDataUltimaAlteracao(Date value) {
        entity.setDataUltimaAlteracao(value);
    }

    /**
     * Getter for observacao.
     *
     * @return value for observacao
     */
    @XmlElement
    public String getObservacao() {
        return (expandLevel > 0) ? entity.getObservacao() : null;
    }

    /**
     * Setter for observacao.
     *
     * @param value the value to set
     */
    public void setObservacao(String value) {
        entity.setObservacao(value);
    }

    /**
     * Getter for logradouro.
     *
     * @return value for logradouro
     */
    @XmlElement
    public String getLogradouro() {
        return (expandLevel > 0) ? entity.getLogradouro() : null;
    }

    /**
     * Setter for logradouro.
     *
     * @param value the value to set
     */
    public void setLogradouro(String value) {
        entity.setLogradouro(value);
    }

    /**
     * Getter for logradouroNumero.
     *
     * @return value for logradouroNumero
     */
    @XmlElement
    public String getLogradouroNumero() {
        return (expandLevel > 0) ? entity.getLogradouroNumero() : null;
    }

    /**
     * Setter for logradouroNumero.
     *
     * @param value the value to set
     */
    public void setLogradouroNumero(String value) {
        entity.setLogradouroNumero(value);
    }

    /**
     * Getter for cep.
     *
     * @return value for cep
     */
    @XmlElement
    public String getCep() {
        return (expandLevel > 0) ? entity.getCep() : null;
    }

    /**
     * Setter for cep.
     *
     * @param value the value to set
     */
    public void setCep(String value) {
        entity.setCep(value);
    }

    /**
     * Getter for bairro.
     *
     * @return value for bairro
     */
    @XmlElement
    public String getBairro() {
        return (expandLevel > 0) ? entity.getBairro() : null;
    }

    /**
     * Setter for bairro.
     *
     * @param value the value to set
     */
    public void setBairro(String value) {
        entity.setBairro(value);
    }

    /**
     * Getter for cidade.
     *
     * @return value for cidade
     */
    @XmlElement
    public String getCidade() {
        return (expandLevel > 0) ? entity.getCidade() : null;
    }

    /**
     * Setter for cidade.
     *
     * @param value the value to set
     */
    public void setCidade(String value) {
        entity.setCidade(value);
    }

    /**
     * Getter for uf.
     *
     * @return value for uf
     */
    @XmlElement
    public String getUf() {
        return (expandLevel > 0) ? entity.getUf() : null;
    }

    /**
     * Setter for uf.
     *
     * @param value the value to set
     */
    public void setUf(String value) {
        entity.setUf(value);
    }

    /**
     * Getter for pais.
     *
     * @return value for pais
     */
    @XmlElement
    public String getPais() {
        return (expandLevel > 0) ? entity.getPais() : null;
    }

    /**
     * Setter for pais.
     *
     * @param value the value to set
     */
    public void setPais(String value) {
        entity.setPais(value);
    }

    /**
     * Getter for logradouroComplemento.
     *
     * @return value for logradouroComplemento
     */
    @XmlElement
    public String getLogradouroComplemento() {
        return (expandLevel > 0) ? entity.getLogradouroComplemento() : null;
    }

    /**
     * Setter for logradouroComplemento.
     *
     * @param value the value to set
     */
    public void setLogradouroComplemento(String value) {
        entity.setLogradouroComplemento(value);
    }

    /**
     * Getter for usuarioCollection.
     *
     * @return value for usuarioCollection
     */
    @XmlElement
    public UsuarioesConverter getUsuarioCollection() {
        if (expandLevel > 0) {
            if (entity.getUsuarioCollection() != null) {
                return new UsuarioesConverter(entity.getUsuarioCollection(), uri.resolve("usuarioCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for usuarioCollection.
     *
     * @param value the value to set
     */
    public void setUsuarioCollection(UsuarioesConverter value) {
        entity.setUsuarioCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for areaAtuacaoId.
     *
     * @return value for areaAtuacaoId
     */
    @XmlElement
    public AreaAtuacaoConverter getAreaAtuacaoId() {
        if (expandLevel > 0) {
            if (entity.getAreaAtuacaoId() != null) {
                return new AreaAtuacaoConverter(entity.getAreaAtuacaoId(), uri.resolve("areaAtuacaoId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for areaAtuacaoId.
     *
     * @param value the value to set
     */
    public void setAreaAtuacaoId(AreaAtuacaoConverter value) {
        entity.setAreaAtuacaoId((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for empresaCollection.
     *
     * @return value for empresaCollection
     */
    @XmlElement
    public EmpresasConverter getEmpresaCollection() {
        if (expandLevel > 0) {
            if (entity.getEmpresaCollection() != null) {
                return new EmpresasConverter(entity.getEmpresaCollection(), uri.resolve("empresaCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for empresaCollection.
     *
     * @param value the value to set
     */
    public void setEmpresaCollection(EmpresasConverter value) {
        entity.setEmpresaCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Getter for matrizId.
     *
     * @return value for matrizId
     */
    @XmlElement
    public EmpresaConverter getMatrizId() {
        if (expandLevel > 0) {
            if (entity.getMatrizId() != null) {
                return new EmpresaConverter(entity.getMatrizId(), uri.resolve("matrizId/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for matrizId.
     *
     * @param value the value to set
     */
    public void setMatrizId(EmpresaConverter value) {
        entity.setMatrizId((value != null) ? value.getEntity() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Empresa entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Empresa getEntity() {
        if (entity.getEmpresaId() == null) {
            EmpresaConverter converter = UriResolver.getInstance().resolve(EmpresaConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Empresa entity.
     *
     * @return an resolved entity
     */
    public Empresa resolveEntity(EntityManager em) {
        List<Usuario> usuarioCollection = entity.getUsuarioCollection();
        List<Usuario> newusuarioCollection = new java.util.ArrayList<Usuario>();
        for (Usuario item : usuarioCollection) {
            newusuarioCollection.add(em.getReference(Usuario.class, item.getUsuarioId()));
        }
        entity.setUsuarioCollection(newusuarioCollection);
        AreaAtuacao areaAtuacaoId = entity.getAreaAtuacaoId();
        if (areaAtuacaoId != null) {
            entity.setAreaAtuacaoId(em.getReference(AreaAtuacao.class, areaAtuacaoId.getAreaAtuacaoId()));
        }
        List<Empresa> empresaCollection = entity.getEmpresaCollection();
        List<Empresa> newempresaCollection = new java.util.ArrayList<Empresa>();
        for (Empresa item : empresaCollection) {
            newempresaCollection.add(em.getReference(Empresa.class, item.getEmpresaId()));
        }
        entity.setEmpresaCollection(newempresaCollection);
        Empresa matrizId = entity.getMatrizId();
        if (matrizId != null) {
            entity.setMatrizId(em.getReference(Empresa.class, matrizId.getEmpresaId()));
        }
        return entity;
    }
}
